//
//  XMGTabBarController.m
//  01-百思不得姐
//
//  Created by xiaomage on 15/7/22.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "XMGTabBarController.h"
#import "WYCNavigationController.h"
#import "WYCMeController.h"
#import "WYCBillController.h"
#import "WYCSummaryController.h"
#import "WYCRepaymentController.h"
#import "ADTabBar.h"
#import "UIColor+ColorHelper.h"
#import "ADAnimationView.h"

@interface XMGTabBarController () <ADTabBarDelegate>

@end

@implementation XMGTabBarController
@synthesize button;



- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createTabBar];
//    [self setup];
//    [self addButtonNotifation];
    
    
    
}
- (void)createTabBar{
    self.tabBar.tintColor = [UIColor whiteColor];
    //更换系统自带的tabbar
    ADTabBar *tabBar = [[ADTabBar alloc]init];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [tabBar setBackgroundImage:img];
    
    [tabBar setShadowImage:img];
    
    tabBar.adDelegate = self;
    //使用kvc，将自定义的tabbar替换掉系统的tabbar
    [self setValue:tabBar forKey:@"tabBar"];
    
    
    //tabbar加阴影
    [self dropShadowWithOffset:CGSizeMake(0, -1) radius:1 color:[UIColor grayColor] opacity:0.3];
    
    
    WYCBillController *homeVC = [[WYCBillController alloc] init];
    WYCRepaymentController *dynamicVC = [[WYCRepaymentController alloc] init];
    WYCSummaryController *msgVC = [[WYCSummaryController alloc] init];
    WYCMeController *mineVC = [[WYCMeController alloc] init];
    
    NSMutableArray *controllerArray = [NSMutableArray arrayWithObjects:homeVC, dynamicVC, msgVC, mineVC, nil];
    
    NSArray *normalImageArray = @[@"账单管理_normal", @"卡包_normal", @"汇总_normal", @"我的_normal"];
    NSArray *selectedImageArray = @[@"账单管理_selected", @"卡包_selected", @"汇总_selected", @"我的_selected"];
    NSArray *titleArray = @[@"首页", @"卡包", @"汇总", @"我的"];
    
    for (int  i = 0; i < controllerArray.count; i++)
    {
        // 分别找到每个视图控制器
        UIViewController *vc = controllerArray[i];
        // 添加标题
        vc.title = titleArray[i];
        // 转化成导航控制器
        WYCNavigationController *navi = [[WYCNavigationController alloc]initWithRootViewController:vc];
        // 将数组array中的视图控制器替换成导航控制器
        [controllerArray replaceObjectAtIndex:i withObject:navi];
        
        // 渲染模式
        UIImage *normalImage = [UIImage imageNamed:normalImageArray[i]];
        UIImage *selectImage = [UIImage imageNamed:selectedImageArray[i]];
        
        navi.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navi.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //tabbar文字字体颜色
        [navi.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:wycMain} forState:UIControlStateSelected];
        
        //文字位置
        [navi.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
        
    }
    self.viewControllers = controllerArray;
    
    
}
//tabbar加阴影
- (void)dropShadowWithOffset:(CGSize)offset radius:(CGFloat)radius color:(UIColor *)color opacity:(CGFloat)opacity
{
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.tabBar.layer.shadowColor = color.CGColor;
    self.tabBar.layer.shadowOffset = offset;
    self.tabBar.layer.shadowRadius = radius;
    self.tabBar.layer.shadowOpacity = opacity;
    self.tabBar.clipsToBounds = NO;
}


#pragma mark - ADTabBarDelegate
- (void)tabBarDidClickCenterButton:(ADTabBar *)tabBar
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    ADAnimationView *adView = [[ADAnimationView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [window addSubview:adView];
    [adView showButtonAnimation];
}
@end
